local qizhi = fk.CreateSkill{
  name = "wk_heg__qizhi",
}

Fk:loadTranslationTable{
  ["wk_heg__qizhi"] = "奇制",
  [":wk_heg__qizhi"] = "与你势力相同的角色于其回合内首次造成伤害后，你可以弃置不为受伤角色的一张牌，然后以此法失去牌的角色摸一张牌。",
  ["#wk_heg__qizhi-choose"] = "奇制：选择一名除受伤角色外的角色，弃置其一张牌",

  ["$wk_heg__qizhi1"] = "声东击西，敌寇一网成擒。",
  ["$wk_heg__qizhi2"] = "吾意不在此地，已遣别部出发。",
}

local H = require "packages/ol_hegemony/util"

qizhi:addEffect(fk.Damage, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if not (player:hasSkill(qizhi.name) and target == player.room.current and H.compareKingdomWith(player, target)) then return false end
    local room = player.room
    local damage_event = room.logic:getCurrentEvent()
    if not damage_event then return false end
    local events = room.logic:getActualDamageEvents(1, function(e)
        return e.data.from == target
      end, Player.HistoryTurn)
    return #events > 0 and damage_event.id == events[1].id
    -- local x = target:getMark("wk_heg__qizhi-turn")
    -- if x == 0 then
    --   room.logic:getEventsOfScope(GameEvent.ChangeHp, 1, function (e)
    --     local reason = e.data.reason
    --     if reason == "damage" then
    --       local first_damage_event = e:findParent(GameEvent.Damage)
    --       if first_damage_event and first_damage_event.data[1].from == target then
    --         x = first_damage_event.id
    --         room:setPlayerMark(target, "wk_heg__qizhi-turn", x)
    --         return true
    --       end
    --     end
    --   end, Player.HistoryTurn)
    -- end
    -- return damage_event.id == x
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function(p) return p ~= data.to and not p:isNude() end)
    local to = room:askToChoosePlayers(player, {
        targets = targets, 
        min_num = 1, 
        max_num = 1, 
        prompt = "#wk_heg__qizhi-choose", 
        skill_name = qizhi.name, 
        cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {to = to[1]})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).to
    if to then
      local id = room:askToChooseCards(player,{
          target = to,
          min = 1,
          max = 1,
          flag = "he",
          skill_name = qizhi.name,
      })
      room:throwCard(id, qizhi.name, to, player)
      if not to.dead then
        to:drawCards(1, qizhi.name)
      end
    end
  end,
})

return qizhi